package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitiveDto;
import com.heima.model.common.pojos.admin.AdSensitive;
import com.heima.wemedia.service.AdminSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/sensitive")
@Slf4j
public class AdminSensitiveController {

    @Autowired
    private AdminSensitiveService adminSensitiveService;

    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id){
        log.info("删除{}敏感词",id);
        return adminSensitiveService.delById(id);
    }


    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitiveDto dto){
        log.info("查询敏感词{}",dto);
        return adminSensitiveService.list(dto);
    }

    /**
     * 保存敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/save")
    //1.先清缓存，再改数据库：重大错误
    //2.一定要先改数据库然后再清缓存：可能存在短时间的数据不一致，最终是一致
    public ResponseResult save(@RequestBody AdSensitive adSensitive){
        log.info("保存敏感词{}",adSensitive);
        return adminSensitiveService.saveById(adSensitive);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdSensitive adSensitive){
        log.info("修改敏感词{}",adSensitive);
        return adminSensitiveService.update(adSensitive);
    }


}
